-module(agent_f5_proxy).

-behaviour(gen_server).

%% API
-export([start_link/0]).

-export([add_domain_mapping/1]).
-export([delete_domain_mapping/1]).

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
         terminate/2, code_change/3]).

-record(state, {}).




add_domain_mapping({Domain,Ip,Port})->
        gen_server:call(?MODULE, { add , {Domain,Ip,Port}},5000).
        

delete_domain_mapping({all , Ip , Port}) ->          
        gen_server:call(?MODULE, { delete , {all,Ip,Port}} , 5000);
delete_domain_mapping({Domain,Ip,Port})->
        gen_server:call(?MODULE, { delete , {Domain,Ip,Port}} , 15000).   
        
%%====================================================================
%% API
%%====================================================================
%%--------------------------------------------------------------------
%% Function: start_link() -> {ok,Pid} | ignore | {error,Error}
%% Description: Starts the server
%%--------------------------------------------------------------------
start_link() ->
    gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

%%====================================================================
%% gen_server callbacks
%%====================================================================

%%--------------------------------------------------------------------
%% Function: init(Args) -> {ok, State} |
%%                         {ok, State, Timeout} |
%%                         ignore               |
%%                         {stop, Reason}
%% Description: Initiates the server
%%--------------------------------------------------------------------
init([]) ->
    {ok, #state{}}.

%%--------------------------------------------------------------------
%% Function: %% handle_call(Request, From, State) -> {reply, Reply, State} |
%%                                      {reply, Reply, State, Timeout} |
%%                                      {noreply, State} |
%%                                      {noreply, State, Timeout} |
%%                                      {stop, Reason, Reply, State} |
%%                                      {stop, Reason, State}
%% Description: Handling call messages
%%--------------------------------------------------------------------
handle_call({add , {Domain,Ip,Port}}=Msg, _From, State) ->
%%        {reply,ok,State } ;
%%script/f5.py  --addmember 172.18.158.1:80 --domain www.app1.com
  %%      error_logger:info_msg("Domain=~p,Ip=~p,Port=~p",[Domain,Ip,Port]),
        error_logger:info_msg("agent_f5_proxy:recving a add msg:~p~n",[Msg]),
        Command = io_lib:format("sh script/f5_add.sh  ~s ~s ~p  " ,
                        [ Domain , Ip , Port ] ),
                        
        case  os:cmd(Command) of
               
                 "2\n" ->
                        error_logger:info_msg("agent_f5_proxy add route success ~n"),
                        { reply , ok , State } ;
                 "0\n" ->
                        error_logger:info_msg("agent_f5_proxy add route success ~n"),
                        { reply , ok , State } ;
                 Any  ->
                        error_logger:info_msg("agent_f5_proxy add route error:~p ~n",[Any]),
                        { reply ,  { error  , { Domain , Ip , Port ,Any } } , State } 
        end;                        
handle_call({delete , {all,Ip,Port}}, _From, State) ->
        
        proc_lib:spawn(fun()->
                Command = io_lib:format("sh script/f5_del_node.sh  ~s ~p  " ,
                        [  Ip , Port ] ),
                        
                 case  os:cmd(Command) of
                         "2\n" ->
                              error_logger:info_msg("f5_del_node success~n");
                         "0\n" ->
                              error_logger:info_msg("f5_del_node success~n");
                         Any ->
                              error_logger:info_msg("f5_del_node error, ~p~n",[Any])

                end
        end),
        
        {reply,ok,State};

                
handle_call({delete , {Domain,Ip,Port}}, _From, State) ->
%%        {reply,ok,State } ;
     Command = io_lib:format("sh script/f5_del.sh  ~s ~s ~p  " ,
                        [ Domain , Ip , Port ] ),
                        
        case  os:cmd(Command) of
                 
                 "2\n" ->
                        { reply , ok , State } ;
                 "0\n" ->
                        { reply , ok , State } ;
                 Any ->
                        { reply , {error , Any} , State }
        end;     

handle_call(_Request, _From, State) ->
    Reply = ok,
    {reply, Reply, State}.

%%--------------------------------------------------------------------
%% Function: handle_cast(Msg, State) -> {noreply, State} |
%%                                      {noreply, State, Timeout} |
%%                                      {stop, Reason, State}
%% Description: Handling cast messages
%%--------------------------------------------------------------------
handle_cast(_Msg, State) ->
    {noreply, State}.

%%--------------------------------------------------------------------
%% Function: handle_info(Info, State) -> {noreply, State} |
%%                                       {noreply, State, Timeout} |
%%                                       {stop, Reason, State}
%% Description: Handling all non call/cast messages
%%--------------------------------------------------------------------
handle_info(_Info, State) ->
    {noreply, State}.

%%--------------------------------------------------------------------
%% Function: terminate(Reason, State) -> void()
%% Description: This function is called by a gen_server when it is about to
%% terminate. It should be the opposite of Module:init/1 and do any necessary
%% cleaning up. When it returns, the gen_server terminates with Reason.
%% The return value is ignored.
%%--------------------------------------------------------------------
terminate(_Reason, _State) ->
    ok.

%%--------------------------------------------------------------------
%% Func: code_change(OldVsn, State, Extra) -> {ok, NewState}
%% Description: Convert process state when code is changed
%%--------------------------------------------------------------------
code_change(_OldVsn, State, _Extra) ->
    {ok, State}.

%%--------------------------------------------------------------------
%%% Internal functions
%%--------------------------------------------------------------------





